package com.joshua.viewtest.demo.RecylerView;

import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.joshua.viewtest.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.ButterKnife;

public class RecylclerStaggeredGridViewAty extends AppCompatActivity {
    private static final int COLUMN_COUNT = 4;

    @BindView(R.id.id_recyclerListView)
    RecyclerView mRecyclerView;
    @BindDimen(R.dimen.divider_height)
    int mDividerHeight;

    private List<String> mDatas;
    private List<Integer> mChildHights;
    private HomeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recylcler_list_view_aty);
        ButterKnife.bind(this);

        initData();
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(COLUMN_COUNT,
                StaggeredGridLayoutManager.VERTICAL));

        adapter = new HomeAdapter();
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addItemDecoration(new DividerStaggeredItemDecoration(
                RecylclerStaggeredGridViewAty.this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        initEvent();
    }

    private void initEvent() {
        adapter.setOnItemClickerListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.staggered_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.id_action_add:
                adapter.addData(0);
                break;
            case R.id.id_action_delete:
                adapter.removeData(0);
                break;
        }
        return true;
    }

    protected void initData() {
        mDatas = new ArrayList<>();
        mChildHights = new ArrayList<>();
        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
            mChildHights.add((int) (100 + Math.random() * 300));
        }
        Point size = new Point();
        this.getWindowManager().getDefaultDisplay().getSize(size);
    }

    interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
        private OnItemClickListener mListener;

        public void setOnItemClickerListener(OnItemClickListener listener) {
            this.mListener = listener;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(RecylclerStaggeredGridViewAty.this)
                    .inflate(R.layout.item_recycler_list_view, parent, false));
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            holder.tv.setText(mDatas.get(position));
            ViewGroup.LayoutParams lp = holder.fl.getLayoutParams();
            lp.height = mChildHights.get(position);
            holder.fl.setLayoutParams(lp);

            if (mListener != null) {
                holder.tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mListener.onItemClick(holder.tv, pos);

                    }
                });
                holder.tv.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mListener.onItemLongClick(holder.tv, pos);
                        removeData(pos);
                        return false;
                    }
                });
            }

        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        public void addData(int position) {
            mDatas.add(position, "Insert One");
            mChildHights.add(position, (int) (100 + Math.random() * 300));
            notifyItemInserted(position);
        }

        public void removeData(int position) {
            mDatas.remove(position);
            mChildHights.remove(position);
            notifyItemRemoved(position);
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.id_num)
            TextView tv;
            @BindView(R.id.id_fl)
            View fl;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
}
